/* global AFRAME, THREE */
AFRAME.registerComponent('sphere-test', {
  schema: {
    sphereTestTemplate: { default: '#sphere-test-template' },
    triggerKeyCode: { default: 'T' } // spacebar
  },

  init: function () {
    const self = this;
    self.sceneEl = self.el.sceneEl;
    document.addEventListener('keyup', function (e) {
      if (e.key == self.data.triggerKeyCode) {
        console.log('sphere-test-template,ballonGenerationController');
        self.ballonGenerationController();
      }
    })

    this.ballonGenerationController = function () {
      const ballon = self.createBalloon();
      addElementToScene(self.sceneEl, ballon);
    },

      this.createBalloon = function () {
        const el = document.createElement('a-entity');
        el.setAttribute('networked', 'template:' + this.data.sphereTestTemplate);
        el.addEventListener('collide', this.shootCollided);
        return el;
      },
      this.shootCollided = function (event) {
        const bodyEl = event.detail.body.el;// Other entity, which playerEl touched.此处为被子弹射中的物体(气球等)
        const targetEl = event.detail.target.el;// Original entity (playerEl).此处为子弹
        console.log('balloon collide detected');
        console.log('bodyEl', bodyEl);
        console.log('targetEl', targetEl);
        if (bodyEl.id === 'floor') {
          console.log('Hit the floor');
          targetEl.removeEventListener('collide', this.shootCollided);
          targetEl.parentNode.removeChild(targetEl);
        }
        // 这个逻辑和子弹碰撞那边逻辑重复了，后续出问题需要修改
        else if (bodyEl.className === 'target') {
          console.log('Hit the target!');
          console.log('bodyEl getAttribute random-score,before', bodyEl.getAttribute('random-score', 'score'));
          setBallonScore(bodyEl);
          console.log('bodyEl getAttribute random-score,after', bodyEl.getAttribute('random-score', 'score'));
          targetEl.removeEventListener('collide', this.shootCollided);
          targetEl.parentNode.removeChild(targetEl);
          bodyEl.parentNode.removeChild(bodyEl);
        }
      },
      this.drop=function(){
        
      }
  },
});
